package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Healthplan;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.vo.CustomMade;
import com.jrtc.base.entity.vo.CustomMades;
import com.jrtc.base.entity.vo.HealthplanVo;
import com.jrtc.base.entity.vo.TmpCustomMadeVo;
import com.jrtc.service.HealthplanService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 康复计划表(Healthplan)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:03
 */
@RestController
@RequestMapping("healthplan")
public class HealthplanController {
    /**
     * 服务对象
     */
    @Resource
    private HealthplanService healthplanService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Healthplan selectOne(Integer id) {
        return this.healthplanService.queryById(id);
    }
    
    @RequestMapping("getHealthplanList")
    public ResultDTO getHealthplanList(@RequestBody Healthplan healthplan)
    {
        IPage<Healthplan> ipage = healthplanService.queryHealthplanList(healthplan);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertHealthplan")
    public ResultDTO insertHealthplan(@RequestBody Healthplan healthplan)
    {
        Healthplan tmpHealthplan = healthplanService.insert(healthplan);
        if (tmpHealthplan != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getHealthplanById")
    public ResultDTO getHealthplanById(@RequestBody Map<String,Integer> map)
    {
        int healthplanId = map.get("healthplanId");
        Healthplan healthplan = healthplanService.queryById(healthplanId);
        if (healthplan==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(healthplan);
    }

    @RequestMapping("updateHealthplan")
    public ResultDTO updateHealthplan(@RequestBody Healthplan healthplan)
    {
        if (healthplan==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (healthplanService.update(healthplan)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteHealthplan")
    public ResultDTO deleteHealthplan(@RequestBody Map<String,Integer> map)
    {
        int healthplanId = map.get("healthplanId");
        if (healthplanService.deleteById(healthplanId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }

    /**
     * 查询指定康复计划
     * */
    @PostMapping("/selectHealthplan")
    public ResultDTO selectHealthplan(@RequestBody Map<String, Object> map){
        if (map.get("healthplanId") != null && !map.get("healthplanId").equals("")){
            Integer healthplanId = Integer.parseInt(map.get("healthplanId").toString());

            HealthplanVo healthplanVo;
            try {
                healthplanVo = healthplanService.selectHealthplan(healthplanId);
            } catch (Exception e){
                return ResultDTOBuilder.failureMsg(e.getMessage());
            }

            if (healthplanVo != null){
                return ResultDTOBuilder.success(healthplanVo);
            }
            else {
                return ResultDTOBuilder.failureMsg("未查到数据");
            }
        }
        else {
            return ResultDTOBuilder.failureMsg("缺少必要参数");
        }
    }

    /**
     * 运动处方模板列表
     * */
    @PostMapping("/healthPlanList")
    public ResultDTO healthPlanList(){
        List<Healthplan> healthplanList = healthplanService.healthPlanList();
        return ResultDTOBuilder.success(healthplanList);
    }

    /**
     * id获取运动处方模板信息
     * */
    @PostMapping("/healthPlanById")
    public ResultDTO healthPlanById(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        Healthplan healthplan = healthplanService.healthPlanById(id);
        return ResultDTOBuilder.success(healthplan);
    }

    /**
     * 定制运动处方模板
     * */
    @PostMapping("/customMade")
    public ResultDTO customMade(HttpServletRequest request, @RequestBody Healthplan healthplan){
        int i;
        try {
            i = healthplanService.customMade(healthplan, request);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }
        if (i > 0){
            return ResultDTOBuilder.success("添加成功");
        }
        else {
            return ResultDTOBuilder.failureMsg("添加失败");
        }
    }

    /**
     * 查询待修改的运功处方模板
     * */
    @PostMapping("/selectCustomMade")
    public ResultDTO selectCustomMade(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        Healthplan healthplan = healthplanService.selectCustomMade(id);
        return ResultDTOBuilder.success(healthplan);
    }

    /**
     * 修改运动处方模板
     * */
    @PostMapping("/saveCustomMade")
    public ResultDTO saveCustomMade(@RequestBody Healthplan healthplan){
        int i;
        try {
            i = healthplanService.saveCustomMade(healthplan);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (i > 0){
            return ResultDTOBuilder.success("修改成功");
        }
        else {
            return ResultDTOBuilder.failureMsg("修改失败");
        }
    }

    /**
     * 运动处方模板列表
     * */
    @PostMapping("/customMades")
    public ResultDTO customMades(HttpServletRequest request, @RequestBody CustomMade customMade){
        TmpCustomMadeVo customMades;
        try {
            customMades = healthplanService.customMades(request, customMade);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        return ResultDTOBuilder.success(customMades);
    }
}